﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Engine;

namespace WindowsGame1
{
    class inGameScene : Scene
    {
        //Actor tipito;
        //Actor tipito2;
        bool isPressed = false;
        InputManager _Inputs;
        Camara mainCamera;
        
       // SpriteFont _Font;

        //-Lista de actores
        public static List<Actor> actors = new List<Actor>();
        public static List<Actor> actorsToAdd = new List<Actor>();
        public static List<Actor> actorsToRemove = new List<Actor>();
        //------------------
        //Lista actores 3D
        public static List<Actor3D> actors3D = new List<Actor3D>();
        public static List<Actor3D> actors3DToAdd = new List<Actor3D>();
        public static List<Actor3D> actors3DToRemove = new List<Actor3D>();
        //------------------

        List<Keys> InputKeys = new List<Keys>();

        Keys[][] Combos = {new Keys[]{Keys.Right, Keys.Right},
                           new Keys[]{Keys.Left, Keys.Right, Keys.S},
                           new Keys[]{Keys.Down, Keys.Right, Keys.A},
                           new Keys[]{Keys.Left, Keys.Down, Keys.Right, Keys.A}};
        
        public inGameScene(ContentManager content)
        {
           /* _Font = Content.Load<SpriteFont>("SpriteFont1");

            tipito = new Actor();
            tipito.anim = new Animation(Content.Load<Texture2D>("f0"), 9, 8, 72, 60);
            //tipito.tex = Content.Load<Texture2D>("tipito");
            tipito.position = new Vector2(100, 100);
            tipito.direction = new Vector2(2, 0);
            tipito.gravity = new Gravity(true);


            //-------------
            tipito2 = new Actor();
            tipito2.anim = new Animation(Content.Load<Texture2D>("f0"), 9, 8, 72, 60);
            tipito2.position = new Vector2(120, 120);
            tipito2.direction = new Vector2(2, 3);
            tipito2.gravity = new Gravity(true);
            //-------------
            */           

            Content = content;

            mainCamera = new Camara();
            _Inputs = new InputManager(InputKeys, Combos);

            InputKeys.Add(Keys.Left);
            InputKeys.Add(Keys.Right);
            InputKeys.Add(Keys.Up);
            InputKeys.Add(Keys.Down);
            InputKeys.Add(Keys.A);
            InputKeys.Add(Keys.S);
        }

        public override void update(int dTime)
        {
            /*tipito.update(dTime);
            tipito.update(dTime);
            tipito.anim.update(dTime);
            tipito.direction += tipito.gravity.AddForce;
            tipito.gravity.update(dTime);
            _Inputs.update(dTime);


            tipito2.update(dTime);
            tipito2.anim.update(dTime);
            tipito2.direction += tipito2.gravity.AddForce;
            tipito2.gravity.update(dTime);
            

            */

            _Inputs.UpdateInput();

            //si colisiona se van
            for (int i = 0; i < actors.Count; i++)
            {
                for (int j = i + 1; j < actors.Count; j++)
                {
                    if (actors[i].checkCollisionSat(actors[i], actors[j]))//getColRect().checkcollisionAABB(actors[j].getColRect()))
                    {
                      //if(actors[i].checkcolllisionsat(actor[j]))
                      //{
                        actors[i].isCollision();//Deshabilitar
                        actors[j].isCollision();
                      //}
                    }
                }
            }

            foreach (Actor act in actors)
            {
                act.update(dTime);

                if (act.readyToexit)
                {
                    actorsToRemove.Add(act);
                }
            }

            foreach (Actor act in actorsToAdd)
            {
                actors.Add(act);
                act.anim = new Animation(Content.Load<Texture2D>("f0"), 9, 8, 72, 60);
                act.position = new Vector2(100, 100);
                act.direction = new Vector2(2, 2);
                act.scale = new Vector2(1, 1);
                act.rotation = 10;
                act.angleVelocity = 0.01f;//Numeros chicos.
                act.gravity = new Gravity(true);                
            }

            foreach (Actor act in actorsToRemove)
            {
                actors.Remove(act);
            }
            actorsToAdd.Clear();
            actorsToAdd.Clear();

            //ACTOR3D
            foreach (Actor3D act in actors3D)
            {
                if (act.readyToexit)
                { 
                    actors3DToRemove.Add(act);
                }
            }       

            foreach (Actor3D act in actors3DToAdd)
            {
                //actors3D.Add(act);

                actors3D.Add(new Actor3D(Content.Load<Model>("Iron Man"), new Vector3(0, -100, 200), new Vector3(1, 1, 1), 160.0f));
                //act.Modelo = new Model();
                //act.Position = new Vector3(0, -100, 200);
                //act.scale = new Vector3(1, 1, 1);
                //act.modelRotation = 160.0f;
                
            }

            foreach (Actor3D act in actors3DToRemove)
            {
                actors3D.Remove(act);
            }               
            //--------------


            base.update(dTime);
        }

        public override void draw(SpriteBatch sb)
        {
            #region Lienas y figuras
            // sb.Draw(tipito.tex, tipito.position, Color.Red);
            /*sb.DrawString(_Font, _Inputs.getBuffer(), Vector2.Zero, Color.Red);
            
            sb.DrawString(_Font, _Inputs.lastPressedCombo, new Vector2(0, 200), Color.Red);
            sb.Draw(tipito.anim.text, tipito.position, tipito.anim.getRect(), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            */
            //Se encarga de crear el box dentro de la textura
            //Lineax.DrawRect(sb, new Vector2(tipito.getColRect().X, tipito.getColRect().Y), new Vector2(tipito.getColRect().X + tipito.getColRect().Width, tipito.getColRect().Y + tipito.getColRect().Height));
            //Lineax.DrawLine(sb, new Vector2(0, 0), new Vector2(100, 100));
            //Lineax.DrawRect(sb, new Vector2(350, 50), new Vector2(200, 200));


            //sb.Draw(tipito2.anim.text, tipito2.position, tipito2.anim.getRect(), Color.White, 0, Vector2.Zero,1, SpriteEffects.None, 0);
            //Lineax.DrawRect(sb, new Vector2(tipito2.getColRect().X, tipito2.getColRect().Y), new Vector2(tipito2.getColRect().X + tipito2.getColRect().Width, tipito2.getColRect().Y + tipito2.getColRect().Height));
            #endregion

            foreach (Actor act in actors)
            {
                sb.Draw(act.anim.text, act.position, act.anim.getRect(), Color.White, act.rotation, new Vector2(act.anim.getRect().Width / 2, act.anim.getRect().Height / 2), act.scale, SpriteEffects.None, 0);
                //Lineax.DrawRect(sb, new Vector2(act.getColRect().X, act.getColRect().Y), new Vector2(act.getColRect().X + act.getColRect().Width, act.getColRect().Y + act.getColRect().Height));
                
                Vector2[] vec = act.getColRect();

                for (int i = 0; i < vec.Length; i++)
                {
                    Lineax.DrawLine(sb, vec[i], vec[i >= vec.Length-1? 0:i+1]);
                }
                    Lineax.DrawRect(sb, act.getAABB());
            }

            foreach (Actor3D act in actors3D)
            {
                Matrix[] transforms = new Matrix[act.Modelo.Bones.Count];
                act.Modelo.CopyAbsoluteBoneTransformsTo(transforms);

                foreach (ModelMesh mesh in act.Modelo.Meshes)
                {

                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(act.modelRotation) * Matrix.CreateTranslation(act.Position);
                        effect.View = Matrix.CreateLookAt(mainCamera.Position, mainCamera.Position + mainCamera.Direction, Vector3.Up);
                        effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), mainCamera.aspectRatio, mainCamera.near, mainCamera.fieldOfView);
                    }
                    mesh.Draw();
                }
            }
            base.draw(sb);
        }

        public override void handleInput()
        {
            MouseState mState = new MouseState();
            mState = Mouse.GetState();
            if (mState.LeftButton == ButtonState.Pressed && isPressed == false)
            {
                //actorsToAdd.Add(new Actor());
                actors3DToAdd.Add(new Actor3D());//Content.Load<Model>("Iron Man"), new Vector3(0, -100, 200), new Vector3(1, 1, 1), 160.0f));
                isPressed = true;
            }
            else if (mState.LeftButton == ButtonState.Released)
            {
                isPressed = false;
            }
            base.handleInput();
        }
    }
}
